<?php

class UserController extends AdminController {

    public function filters() {
        return array(
            'accessControl',
        );
    }

    public function actionView($id) {
        $this->render('view', array(
            'model' => $this->loadModel($id),
        ));
    }

    public function actionCreate() {
        $model = new site_administrator;

        if (isset($_POST['site_administrator'])) {
            $model->administrator_new_password = $_POST['site_administrator']['administrator_new_password'];
            $model->attributes = $_POST['site_administrator'];
            
            //file upload
            $file = CUploadedFile::getInstance($model, 'administrator_image');
            $filename = '';
            if ((is_object($file) && get_class($file) === 'CUploadedFile')) {
                $filename = function_lib::url_title($file->name);
                $model->administrator_image = CUploadedFile::getInstance($model, 'administrator_image');
            }
            
            if ($model->save()) {
                //file upload
                if (is_object($file)) {
                    $model->administrator_image->saveAs(Yii::app()->params['config']['updir']['admin'] . $filename);
                }
                
                $this->redirect(array('view', 'id' => $model->administrator_id));
            }
        }

        $this->render('create', array(
            'model' => $model,
        ));
    }

    public function actionUpdate($id) {
        $model = $this->loadModel($id);
        
        $old_filename = $model->administrator_image;

        if (isset($_POST['site_administrator'])) {
            $model->attributes = $_POST['site_administrator'];
            
            //file upload
            $file = CUploadedFile::getInstance($model, 'administrator_image');
            $filename = '';
            if ((is_object($file) && get_class($file) === 'CUploadedFile')) {
                $filename = function_lib::url_title($file->name);
                $model->administrator_image = CUploadedFile::getInstance($model, 'administrator_image');
                $model->administrator_image = $filename;
            }
            
            if ($model->save()) {
                //file upload
                if (is_object($file)) {
                    $model->administrator_image = CUploadedFile::getInstance($model, 'administrator_image');
                    if($model->administrator_image->saveAs(Yii::app()->params['config']['updir']['admin'] . $filename)) {
                        if(file_exists(Yii::app()->params['config']['updir']['admin'].$old_filename)) {
                            unlink(Yii::app()->params['config']['updir']['admin'].$old_filename);
                        }
                    }
                }
                
                $this->redirect(array('view', 'id' => $model->administrator_id));
            }
        }

        $this->render('update', array(
            'model' => $model,
        ));
    }

    public function actionDelete($id) {
        if (Yii::app()->request->isPostRequest) {
            // we only allow deletion via POST request
            $this->loadModel($id)->delete();

            // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
            if (!isset($_GET['ajax']))
                $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('list'));
        }
        else
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
    }

    public function actionIndex() {
        $dataProvider = new CActiveDataProvider('site_administrator');
        $this->render('index', array(
            'dataProvider' => $dataProvider,
        ));
    }

    public function actionList() {
        $model = new site_administrator('search');
        $model->unsetAttributes();  // clear any default values
        if (isset($_GET['site_administrator']))
            $model->attributes = $_GET['site_administrator'];

        $this->render('list', array(
            'model' => $model,
        ));
    }

    public function loadModel($id) {
        $model = site_administrator::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    protected function performAjaxValidation($model) {
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'site-administrator-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }

}
